package map.asteroids;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.jme.math.FastMath;
import com.jme.scene.Node;

import fileHandling.ModelImporter;
import fileHandling.SettingsLoader;
import gameStates.absGamesStates.AbsIngameState;

import logic.nodes.lod.blocks.Block;
import logic.nodes.nodeSettings.Settings;

public class AsteroidField {
		
	public static HashMap<Integer, Asteroid> allAsteroids = new HashMap<Integer, Asteroid>();
	
	private AsteroidFieldProperties properties;
	private List<ResourceAsteroid> clearAsteroids;
	private Block rootBlock;
	private List<Asteroid> asteroids;
	private int maxAsteroids, currentAsteroids, asteroidsLeft;
	
	public AsteroidField(AsteroidFieldProperties properties, Block rootBlock) {
		this.properties = properties;
		this.rootBlock = rootBlock;
		
		asteroidsLeft = -1;
		maxAsteroids = properties.getAsteroidAmount();
		
		asteroids = new ArrayList<Asteroid>();
		
		clearAsteroids = new ArrayList<ResourceAsteroid>();
	}
	
	public boolean createAsteroids(int percentage) {
		if(currentAsteroids == maxAsteroids) return true;
		
		int nextAsteroids = (int)FastMath.ceil(maxAsteroids / 100f * percentage) + 1;
		
		if(asteroidsLeft != -1 && nextAsteroids > asteroidsLeft) nextAsteroids = asteroidsLeft;
		asteroidsLeft = maxAsteroids - currentAsteroids - nextAsteroids;
		
		AbsIngameState ingameState = rootBlock.getIngameState();
		
		for(int i = 0; i < nextAsteroids; i++) {
			AsteroidProperties props = getNextAsteroidProperties();
			String modelPath = props.getModelPath();
			String[] pathArray = modelPath.split("/");
			String asteroidName = pathArray[pathArray.length - 1];
			Node model = ModelImporter.getModel(modelPath);
			if(model == null) break;
			
			int asteroidID = props.getAsteroidID();
			
			Asteroid asteroid = null;
			if(isResourceField()) {
				int res = props.getResource();
				asteroid = new ResourceAsteroid(modelPath, asteroidName, model, res, this, asteroidID, ingameState);
				clearAsteroids.add((ResourceAsteroid)asteroid);
			} else asteroid = new Asteroid(modelPath, asteroidName, model, this, asteroidID, ingameState);
			
			asteroid.getLocalTranslation().set(props.getPosition());
			asteroid.getLocalRotation().set(props.getRotation());
			asteroid.setLocalScale(props.getScale());
			allAsteroids.put(Integer.valueOf(asteroidID), asteroid);
			asteroids.add(asteroid);
			
			rootBlock.getIngameState().addNode(asteroid);
		}
		
		return currentAsteroids == maxAsteroids;
	}
	
	public AsteroidProperties getNextAsteroidProperties() {
		String propsFile = properties.getFieldPath() + "/asteroid" + currentAsteroids + ".xml";
		Settings props = SettingsLoader.loadSettings(propsFile);
		currentAsteroids++;
		return new AsteroidProperties(props);
	}
	
	public ResourceAsteroid getNextResourceAsteroid() {
		assert(isResourceField());
		if(clearAsteroids.isEmpty()) return null;
		
		ResourceAsteroid nextAsteroid = null;
		for(ResourceAsteroid asteroid : clearAsteroids) {
			if(!asteroid.isEmpty()) {
				nextAsteroid = asteroid;
				break;
			}
		}
		if(nextAsteroid != null) clearAsteroids.remove(nextAsteroid);
		return nextAsteroid;
	}
	
	public int getClearAsteroids() { return clearAsteroids.size(); }
	
	public List<Asteroid> getAsteroids() { return asteroids; }
	
	public AsteroidFieldProperties getProperties() { return properties; }
	
	public boolean isResourceField() { return properties.isResourceField(); }
}